#region License
// Copyright 2010 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System;
using System.Collections.Generic;
using MediaWiki.Common;

namespace MediaWiki.MediaWikiConverter.Exporter
{
  public class StateContext : IDisposable
  {
    private readonly IList<State> _stateStack;
    private readonly State _newState;

    internal StateContext(IList<State> stateStack, State newState)
    {
      _stateStack = stateStack;
      _newState = newState;

      Push();
    }

    private void Push()
    {
      _stateStack.Add(_newState);
    }

    private void Pop()
    {
      bool success = _stateStack.Remove(_newState);
      if (!success)
        throw new Exception("Could not remote {0} from state stack. State not found.".FormatWith(_newState));
    }

    public void Dispose()
    {
      Pop();
    }
  }

  public abstract class State
  {
    public abstract StateType StateType { get; }
  }

  public class HeaderState : State
  {
    public int Level { get; set; }

    public override StateType StateType
    {
      get { return StateType.Heading; }
    }
  }

  public class ListState : State
  {
    public static readonly ListState OrderList = new ListState(StateType.OrderedList);
    public static readonly ListState UnorderedList = new ListState(StateType.UnorderedList);

    private readonly StateType _listStateType;

    private ListState(StateType listStateType)
    {
      _listStateType = listStateType;
    }

    public override StateType StateType
    {
      get { return _listStateType; }
    }
  }

  public class HyperlinkState : State
  {
    public Uri HyperlinkUri { get; set; }
    public bool OpenLink { get; set; }

    public override StateType StateType
    {
      get { return StateType.Hyperlink; }
    }
  }
}